"""
实现思路
1、读取源业务库所有表的见表语句
2、解析原表建表语句的字段和表名  --需要使用正则表达式
3、循环生成datax的json脚本，hive的建表语句，shell脚本
"""

import os
import re

# 类型的映射关系
mapping = {
    "int": "INT",
    "varchar": "STRING",
    "decimal": "DOUBLE",
    "datetime": "STRING",
    "tinytext": "STRING"
}


# 将mysql字段类型转换成hive字段类型
def mysql_type_to_hive_type(mysql_col_type):
    hive_col_type = []

    for mysql_type in mysql_col_type:

        flag = True

        for mysql_type_tmp, hive_type in mapping.items():
            if flag:
                if mysql_type_tmp in mysql_type:
                    hive_col_type.append(hive_type)
                    flag = False

        if flag:
            hive_col_type.append("STRING")

    # 返回hvie的字段类型
    return hive_col_type


if __name__ == '__main__':
    root_dir = "../ods/mysql_ddl/"
    # 1、读取源业务库所有表的见表语句
    mysql_ddl_files = os.listdir(root_dir)

    for ddl_file in mysql_ddl_files:
        # 拼接sql文件的路径
        ddl_path = root_dir + ddl_file

        # 解析表名
        mysql_table_name = ddl_file.split(".")[0]

        # 拼接hive表名
        hive_table_name = f"ods_{mysql_table_name}"

        # 读取文件内容
        with open(ddl_path, mode="r", encoding="utf-8") as file:
            # 读取数据，得到一个字符串的sql语句
            ddl_str = "".join(file.readlines())

            #  re.S： 多行模式
            # 取出所有字段和类型
            col_str = re.search("\((.*)PRIMARY KEY", ddl_str, re.S).group(1)
            col_str = col_str.strip()[:-1].replace("decimal(10,2)", "decimal").replace("`", "")

            # 取出所有的字段名
            col_list = col_str.split(",")
            # 取出字段名
            mysql_col_name = [col.strip().split(" ")[0] for col in col_list]

            # 取出原表字段类型
            mysql_col_type = [col.strip().split(" ")[1] for col in col_list]

            # 将mysql字段类型转换成hive字段类型
            hive_col_type = mysql_type_to_hive_type(mysql_col_type)

            #################生产datax json文件########################

            with open("model/data_json_model.json", mode="r", encoding="utf-8") as json_model:
                # 读取dataxjson模板
                datax_json_model = "".join(json_model.readlines())

            # 替换mysql表名
            datax_json_model = datax_json_model.replace("{mysql_table_name}", f'"{mysql_table_name}"')
            # 替换mysql字段名
            datax_json_model = datax_json_model.replace("{mysql_col_name}",
                                                        ",".join([f'"{col_name}"' for col_name in mysql_col_name]))
            # 替换hive表名
            datax_json_model = datax_json_model.replace("{hive_table_name}", hive_table_name)

            # 构建hive的字段名和类型
            hive_name_type_list = []
            for name, type in zip(mysql_col_name, hive_col_type):
                hive_name_type = '{"name": "%s", "type": "%s"}' % (name, type)
                hive_name_type_list.append(hive_name_type)
            # 替换hive的字段名和字段类型
            datax_json_model = datax_json_model.replace("{hive_col_name_type}", ",\n".join(hive_name_type_list))

            # 保存json文件
            with open(f"../ods/ods_datax/{hive_table_name}.json", mode="w", encoding="utf-8") as file:
                file.write(datax_json_model)
                file.flush()
                file.close()

            ###########################生成hive建表语句################################
            with open("model/hive_ddl_model.sql", mode="r", encoding="utf-8") as ddl_file:
                hive_ddl_model = "".join(ddl_file.readlines())
                # 替换表名
            hive_ddl_model = hive_ddl_model.replace("{hive_table_name}", hive_table_name)
            # 构建hive ddl字段
            hive_ddl_col_name_type = []
            for name, type in zip(mysql_col_name, hive_col_type):
                hive_ddl_col_name_type.append(f"{name} {type}")

            # 替换字段
            hive_ddl_model = hive_ddl_model.replace("{hive_ddl_col_name_type}", ",\n".join(hive_ddl_col_name_type))

            # 保存建表语句
            with open(f"../ods/ddl/{hive_table_name}.sql", mode="w", encoding="utf-8") as file:
                file.write(hive_ddl_model)
                file.flush()
                file.close()

            #####################生成shell脚本###########################
            with open("model/ods_shell_model.sh", mode="r", encoding="utf-8") as sh_file:
                sh_model = "".join(sh_file.readlines())
            # 替换表名
            sh_model = sh_model.replace("{hive_table_name}", hive_table_name)

            # 保存脚本
            with open(f"../ods/bin/{hive_table_name}.sh", mode="w", encoding="utf-8", newline="\n") as file:
                file.write(sh_model)
                file.flush()
                file.close()
